package com.lzp.copyui.ijk

import android.content.pm.ActivityInfo
import android.os.Bundle
import android.view.View
import com.base.BaseActivity
import com.lzp.copyui.R
import com.shuyu.gsyvideoplayer.GSYVideoManager
import com.shuyu.gsyvideoplayer.player.PlayerFactory
import com.shuyu.gsyvideoplayer.utils.OrientationUtils
import kotlinx.android.synthetic.main.gsy_video_player.*
import tv.danmaku.ijk.media.exo2.Exo2PlayerManager


/**
 * @describe:https://github.com/CarGuo/GSYVideoPlayer
 * @Author: lixiaopeng
 * @Date: 12/11/20
 */
class GSYVideoPlayer :BaseActivity(){

    private var isPlay = false
    private var isPause = false

    //private val source1 = "http://9890.vod.myqcloud.com/9890_4e292f9a3dd011e6b4078980237cc3d3.f20.mp4"
    private val source1 = "https://download.niucache.com/static/upload/20201229/d92c0c7a-27b2-486e-be4c-8767a4cd754b.mp4"
   // private val source1 = "file:///storage/emulated/0/DCIM/Camera/VID_20201210_143316.mp4"
    private lateinit var orientationUtils:OrientationUtils

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.gsy_video_player)
    }

    override fun initView() {
        super.initView()
        PlayerFactory.setPlayManager(Exo2PlayerManager::class.java)
        videoPlayer.setUp(source1, false, "测试视频")
        //videoPlayer.setThumbImageView(imageView);
        videoPlayer.titleTextView.visibility = View.VISIBLE;
        //设置返回键
        videoPlayer.backButton.visibility = View.VISIBLE;

        orientationUtils = OrientationUtils(this, videoPlayer)

        videoPlayer.fullscreenButton.setOnClickListener {
            orientationUtils.resolveByClick()
        }
        videoPlayer.setIsTouchWiget(true)
        videoPlayer.backButton.setOnClickListener {
            onBackPressed()
        }
        videoPlayer.startPlayLogic()

    }

    override fun onPause() {
        super.onPause()
        videoPlayer.onVideoPause()
    }

    override fun onResume() {
        super.onResume()
        videoPlayer.onVideoResume()
    }

    override fun onDestroy() {
        super.onDestroy()
        GSYVideoManager.releaseAllVideos()
        orientationUtils.releaseListener()
    }

    override fun onBackPressed() {
        //先返回正常状态
        if (orientationUtils.screenType == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
            videoPlayer.fullscreenButton.performClick()
            return
        }
        //释放所有
        videoPlayer.setVideoAllCallBack(null)
        super.onBackPressed()
    }
}